<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Advertise extends UserBackEndController {
    public function __construct()
    {
        parent::__construct();
        $this->load->library('mypaging');
        $this->load->model('repo/user_advertise_model', 'User_Advertise_Model');
    }

    private function intialPaging($userid, $aWhere, $limit)
    {
        $this->mypaging->setCount($this->User_Advertise_Model->countUserAdById($userid, $aWhere));
        $this->mypaging->setLimit($limit);
        $this->mypaging->setMainURL(mns_quang_cao_paging_url());
        $this->mypaging->setSuffix($this->config->item('url_suffix'));
    }

    public function index($pageIndex=1)
    {
        $aSelect = array('id', 'position', 'anchor', 'start_date', 'end_date', 'content', 'publish_flg', 'deleted_at');
        $aWhere = array('user_id'=>$this->user_id);
        $offset = ($pageIndex - 1) * ADVERTISE_LIMIT;
        $this->intialPaging($this->user_id, $aWhere, ADVERTISE_LIMIT);
        $aAds   = $this->User_Advertise_Model->getUserAdById(
            $this->user_id,
            $aSelect,
            $aWhere,
            ADVERTISE_LIMIT,
            $offset
            );
        $aReturn = array('pageTitle' => 'Quảng cáo', 'aAds'=>$aAds, 'pagingBar'=>$this->mypaging->getBar($pageIndex));
        $this->renderView('backend/shop/advertise/index', $aReturn);
    }

    public function register(){
        $aReturn = array('pageTitle' => 'Đăng kí mới | Quảng cáo');
        $this->renderView('backend/shop/advertise/single', $aReturn);
    }

    public function doRegister(){
        $aResult = array();
        $advertise = $this->input->post('advertise', true);
        $this->form_validation->set_rules('advertise[position]',   'lang:advertise_position',   'trim|required|integer|less_than[15]|xss_clean');
        $this->form_validation->set_rules('advertise[anchor]',     'lang:advertise_anchor',     'trim|required|callback_anchor_check');
        $this->form_validation->set_rules('advertise[start_date]', 'lang:advertise_start_date', 'trim|required|callback_start_date_check');
        $this->form_validation->set_rules('advertise[end_date]',   'lang:advertise_end_date',   'trim|required|callback_end_date_check['.$advertise['start_date'].']');
        $this->form_validation->set_error_delimiters('<li>', '</li>');
        if ($this->form_validation->run() == false) {
            $aResult = array(
                'code' => 0,
                'title'=> $this->lang->line('insert_fail'),
                'text' => '<ul>'.validation_errors().'</ul>'
                );
        } else {
            //handle upload
            $config['upload_path']   = './'.UPLOAD_FOLDER_URL . ADVERTISE_FOLDER_URL;
            $config['allowed_types'] = 'zip|rar';
            $config['max_size']      = '2048';
            $config['encrypt_name']  = true;
            $config['remove_spaces'] = true;
            $this->load->library('upload', $config);
            if ( ! $this->upload->do_upload()) {
                $aResult = array(
                'code'  => 0,
                'title' => $this->lang->line('insert_fail'),
                'text'  => '<ul><li>'.$this->upload->display_errors().'</li></ul>'
                );
                $this->renderJSON($aResult);
                return;
            }
            $data = $this->upload->data();
            $advertise['file_name']  = $data['file_name'];
            $advertise['user_id']    = $this->user_id;
            $advertise['created_at'] = get_current_date();
            $result = $this->User_Advertise_Model->register($advertise);
            if($result){
                $aResult = array(
                    'code' => 1,
                    'text' => $this->lang->line('insert_success')
                );
            } else {
                $aResult = array(
                    'code'  => 0,
                    'title' => $this->lang->line('system_error'),
                    'text'  => $this->lang->line('insert_fail')
                );
            }
        }
        $this->renderJSON($aResult);
    }
}
?>